﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmJiaozhengjilu : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string tbz0, tbz1, tbz2, tbz3, tbz4, tbz5, tbz6, tbz7, tbz8, tbz9;
        string tbzn1, tbzn2, tbzn3, tbzn4, tbzn5;
        string jiaozheng;
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmJiaozhengjilu(bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void btn_Reset校正_Click(object sender, EventArgs e)
        {           
            load校正紀錄();
            ResetTextJiaozhengjilu();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void load校正受理人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_校正_受理人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV受理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV受理人.DataSource = dt;
                //Dav檢定機構.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV受理人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetTextJiaozhengjilu()
        {
            dateTimePicker_from_有效日期.ResetText();
            dateTimePicker_from_檢定日期.ResetText();
            dateTimePicker_to_有效日期.ResetText();
            dateTimePicker_to_檢定日期.ResetText();
            txt受理人.ResetText();
            txt檢定機構.ResetText();
            txt量具名稱.ResetText();
            txt量具編號.ResetText();
        }

        public void load校正紀錄()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_校正_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV校正.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV校正.DataSource = dt;
                dataGV校正.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV校正);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText校正受理人()
        {
            txt受理人代號.ResetText();
            txt受理人名稱.ResetText();
        }

        private void frmJiaozhengjilu_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btnTra.Enabled = false;
                btnAddnewJiaozhengshouli.Enabled = false;
            }
            if (!b量具Search)
            {
                btnSearch校正.Enabled = false;
            }
            if (!b量具Edit)
            {
                btnEditbaofei.Enabled = false;
            }
            if (!b量具Del)
            {
                btnDeleteJiaozhengshouli.Enabled = false;
            }
            load校正紀錄();
            ResetTextJiaozhengjilu();
            load校正受理人();
            ResetText校正受理人();
        }

        private void btnAddnewJiaozhengshouli_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "")
                    {
                        MessageBox.Show("檢定受理人代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt受理人代號.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_校正_受理人_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                    cmd.Parameters.AddWithValue("@姓名", txt受理人名稱.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    load校正受理人();
                    ResetText校正受理人();
                    con.Close();
                    //reset all text
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    load校正受理人();
                    return;
                }
            }
        }

        private void btnEditbaofei_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入校正-受理人代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd2;
                        String sql = "SELECT COUNT(*) FROM 校正_受理人 WHERE 受理人代號=N'" + str + "'";
                        cmd2 = new SqlCommand(sql, con);
                        if ((int)cmd2.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_校正_受理人_修改";
                            cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            //con.Open();
                            cmd2.Parameters.AddWithValue("@受理人代號", str);
                            cmd2.Parameters.AddWithValue("@受理人代號2", txt受理人代號.Text);
                            cmd2.Parameters.AddWithValue("@姓名", txt受理人名稱.Text);
                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            load校正受理人();
                            ResetText校正受理人();
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt受理人代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    load校正受理人();
                    return;
                }
            }
        }

        private void btnDeleteJiaozhengshouli_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入校正-受理人號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入校正-受理人代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_校正_受理人_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            load校正受理人();
                            ResetText校正受理人();
                            con.Close();
                            return;
                        }
                        else
                        {
                            load校正受理人();
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    load校正受理人();
                    con.Close();
                    return;
                }
            }
        }

        private void btnResetJiaozhengshouli_Click(object sender, EventArgs e)
        {            
            load校正受理人();
            ResetText校正受理人();
            txt受理人代號.ResetText();
            txt受理人名稱.ResetText();
        }

        private void dataGV受理人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV受理人);
        }

        private void dataGV受理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人代號.Text = dataGV受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人名稱.Text = dataGV受理人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void tab校正紀錄_SelectedIndexChanged(object sender, EventArgs e)
        {
            PublicClass.setRowNumber(dataGV校正);
            ResetTextJiaozhengjilu();
            PublicClass.setRowNumber(dataGV受理人);
            ResetText校正受理人();
        }


        /// <summary>
        /// 校正紀錄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="PhucTG"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            load校正紀錄();
            ResetTextJiaozhengjilu();
        }

        private void dataGV校正_ColumnDividerDoubleClick(object sender, DataGridViewColumnDividerDoubleClickEventArgs e)
        {
            PublicClass.setRowNumber(dataGV校正);
        }

        private void btnSearch校正_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_校正_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@datefrom", dateTimePicker_from_有效日期.Value);
                cmd.Parameters.AddWithValue("@todate", dateTimePicker_to_有效日期.Value);

                cmd.Parameters.AddWithValue("@datefrom2", dateTimePicker_from_檢定日期.Value);
                cmd.Parameters.AddWithValue("@todate2", dateTimePicker_to_檢定日期.Value);

                if (txt量具編號.Text != "") cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                if (txt量具名稱.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                //if (dateTimePicker_from_有效日期.Text != "") cmd.Parameters.AddWithValue("@有效日期From", DateTime.Parse(dateTimePicker_from_有效日期.Text));
                //if (dateTimePicker_to_有效日期.Text != "") cmd.Parameters.AddWithValue("@有效日期To", DateTime.Parse(dateTimePicker_to_有效日期.Text));
                //if (dateTimePicker_from_檢定日期.Text != "") cmd.Parameters.AddWithValue("@檢定日期From", DateTime.Parse(dateTimePicker_from_檢定日期.Text));
                //if (dateTimePicker_to_檢定日期.Text != "") cmd.Parameters.AddWithValue("@檢定日期To", DateTime.Parse(dateTimePicker_to_檢定日期.Text));                
                //if (txt檢定機構.Text != "") cmd.Parameters.AddWithValue("@檢定機構", txt檢定機構.Text);
                if (txt受理人.Text.ToString() != "") cmd.Parameters.AddWithValue("@受理人代號", txt受理人.Text);

                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("項次");
                dt.Columns.Add("量具編號");
                dt.Columns.Add("量具狀態");
                dt.Columns.Add("量具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("存放地點");
                dt.Columns.Add("測量範圍");
                dt.Columns.Add("數量");
                dt.Columns.Add("有效日期From");
                dt.Columns.Add("有效日期To");
                dt.Columns.Add("檢定日期From");
                dt.Columns.Add("檢定日期To");
                dt.Columns.Add("檢定機構");
                dt.Columns.Add("受理人代號");
                dt.Columns.Add("備註");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["項次"] = sqlreader["項次"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具名稱"] = sqlreader["量具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["存放地點"] = sqlreader["存放地點"];
                    dtrows["測量範圍"] = sqlreader["測量範圍"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["有效日期From"] = sqlreader["有效日期From"];
                    dtrows["有效日期To"] = sqlreader["有效日期To"];
                    dtrows["檢定日期From"] = sqlreader["檢定日期From"];
                    dtrows["檢定日期To"] = sqlreader["檢定日期To"];
                    dtrows["檢定機構"] = sqlreader["檢定機構"];
                    dtrows["受理人代號"] = sqlreader["受理人代號"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                    //k++;
                }
                dataGV校正.DataSource = dt;
                PublicClass.setRowNumber(dataGV校正);
                dataGV校正.Columns[0].Width = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnTra_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                
                jiaozheng = "2";
                int r = dataGV校正.CurrentCell.RowIndex;
                string id0 = dataGV校正.Rows[r].Cells[0].Value.ToString();
                string id1 = dataGV校正.Rows[r].Cells[1].Value.ToString();
                string id2 = dataGV校正.Rows[r].Cells[2].Value.ToString();
                string id3 = dataGV校正.Rows[r].Cells[3].Value.ToString();
                string id4 = dataGV校正.Rows[r].Cells[4].Value.ToString();
                string id5 = dataGV校正.Rows[r].Cells[5].Value.ToString();
                string id6 = dataGV校正.Rows[r].Cells[6].Value.ToString();
                string id7 = dataGV校正.Rows[r].Cells[7].Value.ToString();
                string id8 = dataGV校正.Rows[r].Cells[8].Value.ToString();
                string id9 = dataGV校正.Rows[r].Cells[9].Value.ToString();
                string id10 = dataGV校正.Rows[r].Cells[10].Value.ToString();
                string id11 = dataGV校正.Rows[r].Cells[11].Value.ToString();
                string id12 = dataGV校正.Rows[r].Cells[12].Value.ToString();
                string id13 = dataGV校正.Rows[r].Cells[13].Value.ToString();
                string id14 = dataGV校正.Rows[r].Cells[14].Value.ToString();
                tbz0 = id0;
                tbz1 = id1;
                tbz2 = id2;
                tbz3 = id3;
                tbz4 = id4;
                tbz5 = id5;
                tbz6 = id6;
                tbz7 = id7;
                tbz8 = id8;
                tbz9 = id9;
                tbzn1 = id10;
                tbzn2 = id11;
                tbzn3 = id12;
                tbzn4 = id13;
                tbzn5 = id14;

                frmJiaoZheng frm = new frmJiaoZheng(jiaozheng, tbz0, tbz1, tbz2, tbz3, tbz4, tbz5, tbz6, tbz7, tbz8, tbz9, tbzn1, tbzn2, tbzn3, tbzn4, tbzn5, b量具Search, b量具Insert, b量具Edit, b量具Del);
                frm.ShowDialog();
                load校正紀錄();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void txt受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 校正_受理人 where 受理人代號=N'{0}'", txt受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName受理人.Text = "";
                else lbName受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btn受理人Search_Click(object sender, EventArgs e)
        {
            frmSearchJiaoZhengShouliRen frm = new frmSearchJiaoZhengShouliRen(txt受理人);
            frm.ShowDialog();
        }
        
    }
}
